using System.IO;
using FluentNHibernate.AutoMap;
using FluentNHibernate.Cfg;
using FluentNHibernate.Cfg.Db;
using LuceneSearch.ContactApp.Entities;
using NHibernate;
using NHibernate.Tool.hbm2ddl;
using StructureMap;

namespace LuceneSearch.ContactApp.Bootstrapping
{
    public class Bootstrapper
    {
        public void Initialize()
        {
            if(File.Exists("db"))
                File.Delete("db");

            var sessionFactory = Fluently.Configure()
                .Database(SQLiteConfiguration.Standard.UsingFile("db"))
                .Mappings(m =>  m.AutoMappings.Add(
                                    AutoPersistenceModel
                                        .MapEntitiesFromAssemblyOf<Contact>()  
                                        .Where(type => type.Namespace.EndsWith("Entities"))))
                .ExposeConfiguration(config => new SchemaExport(config).Create(false, true))
                .BuildSessionFactory();

            ObjectFactory.Initialize(x =>
                                         {
                                             x.AddRegistry<ObjectRegisty>();
                                             x.ForRequestedType<ISessionFactory>()
                                                 .TheDefault.IsThis(sessionFactory);
                                         });

            ObjectFactory.GetInstance<DataPop>().PopulateSampleData();
        }
    }
}